package mysql

import "gorm.io/gorm"

type Good struct {
	gorm.Model
	UserId     int     `gorm:"type:int;not null;comment:'用户Id'"`
	GoodsTitle string  `gorm:"type:varchar(50);not null;comment:'商品标题'"`
	GoodsStock int     `gorm:"type:int;not null;comment:'商品库存'"`
	GoodsImage string  `gorm:"type:varchar(255);not null;comment:'商品图片'"`
	GoodsPrice float64 `gorm:"type:double(10,2);not null;comment:'商品价格'"`
	GoodsLike  int     `gorm:"type:int;not null;comment:'点赞数量'"`
	Status     int     `gorm:"type:int;not null;comment:状态:1-审核,2-未审核"`
}

func (g *Good) GetGood(page, pageSize int) (list []Good, err2 error) {
	DB.Order("goods_like desc").Offset((page - 1) * pageSize).Limit(pageSize).Find(&list)
	return
}

func (g *Good) GetShow(id int) (show []Good, err2 error) {
	DB.Where("id = ?", id).Limit(1).Find(&show)
	return
}

func (g *Good) Get(id int) error {
	return DB.Where("id = ?", id).Limit(1).Find(&g).Error
}

func (g *Good) Create() error {
	return DB.Create(&g).Error
}
func (g *Good) GoodList(page, pageSize, status int) (good []Good, err2 error) {
	if status != 0 {
		DB.Where("status = ?", status).Offset((page - 1) * pageSize).Limit(pageSize).Find(&good)
	} else {
		DB.Offset((page - 1) * pageSize).Limit(pageSize).Find(&good)
	}
	return
}

func (g *Good) UpdateGood(id, status int) error {
	return DB.Model(&g).Where("id = ?", id).Update("status", status).Error
}
